package zju.cst.seckill.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;


    public <T>boolean exists(KeyPrefix prefix,String key){
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            String realKey = prefix.getPrefix()+key;
            return jedis.exists(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    public <T>T get(KeyPrefix prefix,String key,Class<T> Clazz){
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            String realKey = prefix.getPrefix()+key;
            String str=jedis.get(realKey);
            T t=stringToBean(str,Clazz);
            return t;
        }finally {
            returnToPool(jedis);
        }
    }

    public <T> Boolean set(KeyPrefix prefix,String key, T value){
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            String str=beanToString(value);
            if (str == null || str.length()<=0){
                return false;
            }
            //生成真正的key
            String realKey = prefix.getPrefix()+key;
            int seconds = prefix.expireSeconds();
            if (seconds <= 0){
                jedis.set(realKey,str);
            }else {
                jedis.setex(realKey,seconds,str);
            }
            jedis.set(realKey,str);
            return true;
        }finally {
            returnToPool(jedis);
        }
    }

    public <T> Long incr(KeyPrefix prefix,String key){
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
           String realKey = prefix.getPrefix()+key;

            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }
    public <T> Long decr(KeyPrefix prefix,String key){
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource();
            String realKey = prefix.getPrefix()+key;
            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }


    private <T> String beanToString(T value){
        if (value==null){
            return null;
        }
        Class<?> cla=value.getClass();
        if (cla==int.class||cla==Integer.class || cla == long.class || cla == Long.class){
            return ""+value;
        }else if (cla == String.class){
            return value+"";
        }
        else{
            return JSON.toJSONString(value);
        }

    }


    private <T> T stringToBean(String str,Class<T> cla){
        if (str==null||str.length()<=0||cla==null){
            return null;
        }
        if (cla==int.class||cla==Integer.class){
            return (T) Integer.valueOf(str);
        }else if (cla == String.class){
            return (T)str;
        }else {
            return JSON.toJavaObject(JSON.parseObject(str),cla);
        }

    }

    private void returnToPool(Jedis jedis){
        if (jedis!=null) {
            jedis.close();
        }
    }

}
